Assignment description

Work through the following 18 R problems. This assignment is worth 8 points (4/9 points per problem). You should also complete Assignment 3.2P OR Assignment 3.2R for an additional 2 points. The total of Assignments 3.1 and your choice of either 3.2P (Python) or 3.2R (R challenge) are worth 10 points and 15% of your final course grade (just like the other assignments). You must work individually, but feel free to ask questions in class and on Slack!

You must submit the problems as an R script (.R file). All answers must have comments and be labeled with the problem number. Some answers will only be comments without associated R code. For questions about output, report your output in a comment under the relevant line of code. Note that all comments have # in front of them so that R does not run this line of code.

This week’s assignment has a lot of exercises that you can tackle in different ways. So if you want to use a different approach or package than your classmates, that is fine! We also do not mind what colors you pick for your plots or if font sizes slightly differ. However, always make sure that you practice good, clear, and efficient coding style.

#Q1.1.0
#This is the answer the example problem Q1.1.0 in a comment
ThisVariable <- 1
ThisVariable
#[1] 1

#Q1.1.1
#Answer

Problems

Q3.1.1

Simulate some data and show them in a boxplot.

Q3.1.2

Make a scatterplot of the average temperature measured at Schiphol Airport over the last 70 years. The data can be found here: https://bit.ly/3GLVQ86 . Put time on the x-axis and average temperature (TAVG) on the y axis.

Q3.1.3

The day that the titanic sank was a bad day in many ways. Most importantly, because it helps present-day men to justify pro-male sexism with plots like this:

Can you recreate the barplot with the ggplot2 and titanic packages (dataset titanic_train has the passenger data)? Functions that might be useful are factor() and labs(fill = “my label”).

Q3.1.4

Try out different themes from ggplot for the previous plot. You can find ggplot’s “complete themes” here: https://ggplot2-book.org/polishing.html under point 18.2. Apply the one that you like best.

Q3.1.5

Improve the visuals of this plot in three ways and briefly list your edits in a comment.

plot(mtcars$cyl, mtcars$hp)

Q3.1.6

Use the built-in Orange dataset and recreate this ggplot. Make sure the bars are shown in the same order as here. You might need to google how to reorder them (it is a common nuisance). Notice that you have to compute the trees’ maximum circumference before plotting.

Q3.1.7

Recreate this plot from the Orange dataset. To prevent likely questions: Yes, the plotted line is based on all the data (i.e., not just a single tree).

Q3.1.8

Add a second plot next to the plot from exercise Q3.1.6 showing the development of the individual trees’ circumferences over time. An easy way to do it is with the patchwork package but you can also use the par() function.

Q3.1.9

Ever wondered if guinea pigs’ teeth grow better when they take their vitamin C through orange juice rather than meds? Make a plot that answers this question with the ggstatsplot package using the built-in dataset ToothGrowth. Hint when checking out the package: this is a BETWEEN subject comparison ;)

Q3.1.10

Recreate this 3D plot with the plotly package and the built-in iris dataset.

Q3.1.11

Recreate this animated plot with the gganimate package. You can get the data through the refresh_coronavirus_jhu() function from the package coronavirus. The exact animation speed and other visual settings do not matter for grading.

Q3.1.12

Download some stock prices with the getSymbols function (common symbols are “GOOG”, “AMZN”, or “AAPL”). Plot the first column of the returned object with the plot() function. Why does the output look so fancy despite using the basic plot() function?

Q3.1.13

Define a function called plot2021 which takes a stocksymbol as a text input and plots the stock’s price (i.e., first column as in exercise Q3.1.12) between the 1st of January and the 31st of December. Test it with: plot2021("GOOG")

Q3.1.14

Look through your code for the first eleven exercises. Try to improve the style of two solutions. Describe your edits here (in a comment).

Q3.1.15

Look at this function. Write very briefly what its purpose is (running it and adding printouts helps) and name exactly thee reasons why the code has very poor style (there are more).

w = "apple"
v = function(x){
  y <- strsplit(x, " ", )
  v <- 0
  for(z in unlist(y)){if(z == w){v = v +1}}
  if(v > 0) return(T)
}
v("i bought two bananas and an apple")
## [1] TRUE

Q3.1.16

Look at this lengthy way to construct a matrix. Can you achieve the same with a single line of code shorter than the first line of the code below?

my_matrix <- matrix(c(1, 4, 7, 2, 5, 8, 3, 6, 9), 3, 3)
for(row in 1:nrow(my_matrix)){
  if(row == 1){
    my_matrix[row,] <- my_matrix[row,] * 1
  }else if(row == 2){
    my_matrix[row,] <- my_matrix[row,] * 2
  }else{
    my_matrix[row,] <- my_matrix[row,] * 3
  }
}
my_matrix
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    8   10   12
## [3,]   21   24   27

Q3.1.17

Recursion is one of the most elegant coding tricks. Show what this function does by replacing the recursion (i.e., the internal reuse of the outer function) with a for loop.

recursive_function <- function(x) {
  if (x == 0)    return (1)
  else           return (x * recursive_function(x-1))
}
recursive_function(3)
## [1] 6

Q3.1.18

Make a meme in RStudio about R coding. You can (but do not have to) use the memer package (installation requires install_github() function from the devtools package). Indicate in a comment whether you are fine with us posting the meme anonymously to the meme channel on Slack. The person contributing the meme getting most likes will get a repost + credit in the following years that this course is given!